<%@page import="org.yate.oa.model.LoginEntity"%>
<%@page import="java.text.SimpleDateFormat, java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set value="${pageContext.request.contextPath}" var="ctx" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<link href="${ctx}/css/skin.css" rel="stylesheet" type="text/css" />
<link href="${ctx}/css/chosen.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="${ctx}/css/zTreeStyle.css" type="text/css">
<link href="${ctx}/css/page.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="${ctx}/js/jquery.js"></script>
<script type="text/javascript" src="${ctx}/js/backOffice.js"></script>
<script type="text/javascript"
	src="${ctx}/js/jquery.ztree.core-3.5.min.js"></script>
<script type="text/javascript" src="${ctx}/js/jquery.myPagination.js"></script>
<script type="text/javascript" src="${ctx}/js/chosen.jquery.min.js"></script>
<script type="text/javascript">
	var userId = '';
	var setting = {
		view : {
			dblClickExpand : false,
			showLine : true,
			selectedMulti : false
		},
		data : {
			simpleData : {
				enable : true,
				idKey : "id",
				pIdKey : "parentOrgId",
				rootPId : ""
			},
			key : {
				name : 'orgName'
			}
		},
		callback : {
			beforeClick : function(treeId, treeNode) {
				var zTree = $.fn.zTree.getZTreeObj("tree");
				if (treeNode.isParent) {
					zTree.expandNode(treeNode);
					return true;
				}
			},
			onClick : function(event, treeId, treeNode, clickFlag) {
				var zTree = $.fn.zTree.getZTreeObj("tree");
				orgId = zTree.getSelectedNodes()[0].id;
				$("#hidSelectedOrgId").val(orgId);
			}
		}
	};

	function init() {
		var obj = {};
		obj.type = 'all';
		$.ajax({
			url : '${ctx}/org/post',
			type : 'post',
			data : obj,
			dataType : 'json',
			success : function(data) {
				var t = $("#tree");
				t = $.fn.zTree.init(t, setting, data.data);
			}
		});
	}

	function init2() {
		var obj = {};
		obj.type = "getall";
		$.ajax({
			url : '${ctx}/user/post',
			type : 'post',
			data : obj,
			dataType : 'json',
			success : function(data) {
				if (data.code == 200) {
					var rows = data.data;
					var str = '';
					for ( var i = 0; i < rows.length; i++) {
						str += '<option value="'+rows[i].id+'">'
								+ rows[i].trueName + '</option>';
					}
					$("#selUsers").append(str);
					$("#selUsers").chosen();
					$("#selUsers").change(function() {
						getUserLeave();
					});
				} else {

				}
			}
		});
	}

	$(document).ready(function() {
		init();
		init2();
	});

	function getUserLeave() {
		var obj = {};
		obj.type = "getUserLeave";
		obj.userId = $.trim($("#selUsers").val());
		$.ajax({
			type : 'post',
			url : '${ctx}/leave/post',
			data : obj,
			dataType : 'json',
			success : function(data) {
				if (data.status == 200) {
					$.msgbox.show({
						message : '查询成功',
						icon : 'ok'
					});
					userId = data.data.userEntity.id;
					$('#leaveDay').val(data.data.leaveDay);
					$('#restDay').val(data.data.restDay);
				} else {
					$.msgbox.show({
						message : data.text,
						icon : 'no'
					});
				}
			}
		});
	}

	function queryByUserId() {
		var zTree = $.fn.zTree.getZTreeObj("tree");
		if (zTree.getSelectedNodes().length == 0) {
			$.msgbox.show({
				message : '请选择组织',
				icon : 'no'
			});
			return;
		}
		var orgId = zTree.getSelectedNodes()[0].id;
		var userName = $.trim($('#userName').val());
		if (userName == '') {
			$.msgbox.show({
				message : '请输入要调整的员工名称',
				icon : 'no'
			});
			return;
		}
		var obj = {};
		obj.type = 'get';
		obj.orgId = orgId;
		obj.trueName = userName;
		$.ajax({
			type : 'post',
			url : '${ctx}/leave/post',
			data : obj,
			dataType : 'json',
			success : function(data) {
				if (data.status == 200) {
					$.msgbox.show({
						message : '查询成功',
						icon : 'ok'
					});
					userId = data.data.userEntity.id;
					$('#leaveDay').val(data.data.leaveDay);
					$('#restDay').val(data.data.restDay);
				} else {
					$.msgbox.show({
						message : data.text,
						icon : 'no'
					});
				}
			}
		});
	}

	function update() {
		var leaveDay = $.trim($('#leaveDay').val());
		var restDay = $.trim($('#restDay').val());
		if (userId == '') {
			$.msgbox.show({
				message : '请输入要调整的员工名称',
				icon : 'no'
			});
			return;
		}
		if (leaveDay == '') {
			$.msgbox.show({
				message : '请输入要调至的调休',
				icon : 'no'
			});
			return;
		}
		if (restDay == '') {
			$.msgbox.show({
				message : '请输入要调至的年假',
				icon : 'no'
			});
			return;
		}
		var obj = {};
		obj.type = 'update';
		obj.userId = userId;
		obj.leaveDay = leaveDay;
		obj.restDay = restDay;
		$.ajax({
			type : 'post',
			url : '${ctx}/leave/post',
			data : obj,
			dataType : 'json',
			success : function(data) {
				if (data.status == 200) {
					$.msgbox.show({
						message : '修改成功',
						icon : 'ok'
					});
				}
			}
		});
	}

	function clearOldRestdays() {
		var curMonth = new Date().getMonth() + 1;
		var orgId = $("#hidSelectedOrgId").val();
		var nod = $.fn.zTree.getZTreeObj("tree").getSelectedNodes()[0].orgName;
		if (confirm("您确定清空'"+nod+"'上个季度的调休天数吗？")) {
			$("#aClear").hide();
			$("#clearWait").show();
			var obj = {};
			obj.type = 'clear';
			obj.orgId = orgId;
			$.ajax({
				type : 'post',
				url : '${ctx}/leave/post',
				data : obj,
				dataType : 'json',
				success : function(data) {
					if (data.status == 200) {
						$.msgbox.show({
							message : data.msg,
							icon : 'ok'
						});
					} else {
						$.msgbox.show({
							message : data.msg,
							icon : 'no'
						});
					}
				}
			});
			$("#clearWait").hide();
			$("#aClear").show();
		}
	}
</script>
</head>
<body style="font-size: 62.5%;">
	<table width="100%" border="0" cellpadding="0" cellspacing="0">
		<tr>
			<td width="17" height="29" valign="top"
				background="${ctx}/images/mail_leftbg.gif"><img
				src="${ctx}/images/left-top-right.gif" width="17" height="29" /></td>
			<td width="" height="29" valign="top"
				background="${ctx}/images/content-bg.gif"><table width="100%"
					height="31" border="0" cellpadding="0" cellspacing="0"
					class="left_topbg" id="table2">
					<tr>
						<td height="31"><div class="titlebt">年假数据</div></td>
					</tr>
				</table></td>
			<td width="16" valign="top"
				background="${ctx}/images/mail_rightbg.gif"><img
				src="${ctx}/images/nav-right-bg.gif" width="16" height="29" /></td>
		</tr>
		<tr>
			<td height="71" valign="middle"
				background="${ctx}/images/mail_leftbg.gif">&nbsp;</td>
			<td valign="top" bgcolor="#F7F8F9"><table width="100%"
					height="138" border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td height="13" valign="top">&nbsp;</td>
					</tr>
					<tr>
						<td valign="top"><table width="98%" border="0" align="center"
								cellpadding="0" cellspacing="0">
								<tr>
									<td class="left_txt">当前位置：年假数据</td>
								</tr>
								<tr>
									<td height="20"><table width="100%" height="1" border="0"
											cellpadding="0" cellspacing="0" bgcolor="#CCCCCC">
											<tr>
												<td></td>
											</tr>
										</table></td>
								</tr>
								<tr>
									<td><table width="100%" height="31" border="0"
											cellpadding="0" cellspacing="0" class="nowtable">
											<tr>
												<td class="left_bt2">&nbsp;&nbsp;&nbsp;&nbsp; <a
													href="leaveList.jsp">查看所有员工年假</a>&nbsp;&nbsp;&nbsp;&nbsp; <a
													href="javascript:void(0);" onclick="clearOldRestdays()"
													id="aClear">一键清空上个季度剩余的调休</a> <img
													src="${ctx }/images/wait.gif" id="clearWait"
													style="display: none;" /> <input id="hidSelectedOrgId"
													type="hidden" value="0" />
												</td>
											</tr>
										</table></td>
								</tr>
								<tr>
									<td class="left_txt2">
										<table width="100%" border="0" cellspacing="0" cellpadding="0"
											class="tblOrg" height="200px;">
											<tr>
												<td width="40%" valign="top">
													<ul id="tree" class="ztree"
														style="width: 260px; overflow: auto;"></ul>
												</td>
												<td valign="top">
													<table width="100%" border="0" cellspacing="0"
														cellpadding="0" class="" height="100%">
														<tr>
															<td height="50" valign="middle">请输入员工名称： <input
																type="text" id="userName" /> <input type="button"
																onclick="queryByUserId()" value="查询" /> <br /> <br />
																快速选择：<select id="selUsers"></select>
															</td>
														</tr>
														<tr>
															<td valign="top"><br />
																&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;调整调休至： <input
																type="text" id="restDay" /> <br> <br>
																&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;调整年假至： <input
																type="text" id="leaveDay" /> <br /> <br /> <input
																type="button" value="提交" onclick="update()" /></td>
														</tr>
													</table>
												</td>
											</tr>
										</table>
									</td>
								</tr>
							</table></td>
					</tr>
				</table></td>
			<td background="${ctx}/images/mail_rightbg.gif">&nbsp;</td>
		</tr>
		<tr>
			<td valign="middle" background="${ctx}/images/mail_leftbg.gif"><img
				src="${ctx}/images/buttom_left2.gif" width="17" height="17" /></td>
			<td height="17" valign="top"
				background="${ctx}/images/buttom_bgs.gif"><img
				src="${ctx}/images/buttom_bgs.gif" width="17" height="17" /></td>
			<td background="${ctx}/images/mail_rightbg.gif"><img
				src="${ctx}/images/buttom_right2.gif" width="16" height="17" /></td>
		</tr>
	</table>
</body>
</html>